@model ERP.ERPSystem.ProductionSystem.Dto.ProcessDTO
@*
    编辑过程检验页面
*@
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>编辑过程检验</title>
    <link href="~/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <style>
        .layui-form {
            padding: 20px;
            max-width: 800px;
            margin: 0 auto;
        }
        .layui-form-label {
            width: 120px;
        }
        .layui-input-block {
            margin-left: 150px;
        }
        .form-buttons {
            text-align: center;
            margin-top: 30px;
        }
        .required::before {
            content: '* ';
            color: red;
        }
    </style>
</head>
<body>
    <div class="layui-container">
        <div class="layui-card">
            <div class="layui-card-header">
                <h2>编辑过程检验</h2>
            </div>
            <div class="layui-card-body">
                @Html.AntiForgeryToken()
                <form class="layui-form" lay-filter="processForm">
                    <input type="hidden" name="Id" value="@Model.Id">
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">检验单编号</label>
                        <div class="layui-input-block">
                            <div style="display: flex; align-items: center;">
                                <input type="text" name="ProcessCode" value="@Model.ProcessCode" lay-verify="required" placeholder="请输入检验单编号" autocomplete="off" class="layui-input" style="flex: 1; margin-right: 10px;">
                                <button type="button" class="layui-btn layui-btn-primary layui-btn-sm" onclick="generateProcessCode()" title="自动生成检验单编号">
                                    <i class="layui-icon layui-icon-refresh"></i> 自动生成
                                </button>
                            </div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">检验类型</label>
                        <div class="layui-input-block">
                            <select name="ProcessType" lay-verify="required">
                                <option value="">请选择检验类型</option>
                                @if (Model.ProcessType == "首件检验")
                                {
                                    <option value="首件检验" selected>首件检验</option>
                                }
                                else
                                {
                                    <option value="首件检验">首件检验</option>
                                }
                                @if (Model.ProcessType == "过程检验")
                                {
                                    <option value="过程检验" selected>过程检验</option>
                                }
                                else
                                {
                                    <option value="过程检验">过程检验</option>
                                }
                                @if (Model.ProcessType == "末件检验")
                                {
                                    <option value="末件检验" selected>末件检验</option>
                                }
                                else
                                {
                                    <option value="末件检验">末件检验</option>
                                }
                                @if (Model.ProcessType == "巡回检验")
                                {
                                    <option value="巡回检验" selected>巡回检验</option>
                                }
                                else
                                {
                                    <option value="巡回检验">巡回检验</option>
                                }
                                @if (Model.ProcessType == "终检")
                                {
                                    <option value="终检" selected>终检</option>
                                }
                                else
                                {
                                    <option value="终检">终检</option>
                                }
                            </select>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">工单编号</label>
                        <div class="layui-input-block">
                            <div style="position: relative;">
                                <input type="text" name="OrderCode" value="@Model.OrderCode" lay-verify="required" placeholder="请输入工单编号" autocomplete="off" class="layui-input">
                                <i class="layui-icon layui-icon-search" style="position: absolute; right: 10px; top: 50%; transform: translateY(-50%); cursor: pointer; color: #999;" onclick="selectWorkOrder()" title="选择工单"></i>
                            </div>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">产品物料编码</label>
                        <div class="layui-input-block">
                            <input type="text" name="ProductCode" value="@Model.ProductCode" lay-verify="required" placeholder="请输入产品物料编码" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">产品物料名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="ProductName" value="@Model.ProductName" lay-verify="required" placeholder="请输入产品物料名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">规格型号</label>
                        <div class="layui-input-block">
                            <input type="text" name="Specifications" value="@Model.Specifications" placeholder="请输入规格型号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">单位</label>
                        <div class="layui-input-block">
                            <input type="text" name="Unitt" value="@Model.Unitt" placeholder="请输入单位" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">检测数量</label>
                        <div class="layui-input-block">
                            <input type="number" name="TestingNum" value="@Model.TestingNum" lay-verify="required|number" placeholder="请输入检测数量" autocomplete="off" class="layui-input" min="1">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">检测日期</label>
                        <div class="layui-input-block">
                            <input type="text" name="TestingDate" id="testingDate" value="@Model.TestingDate.ToString("yyyy-MM-dd HH:mm:ss")" lay-verify="required" placeholder="请选择检测日期" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">检测结果</label>
                        <div class="layui-input-block">
                            <select name="TestingResult" lay-verify="required">
                                <option value="">请选择检测结果</option>
                                @if (Model.TestingResult == "检验通过")
                                {
                                    <option value="检验通过" selected>检验通过</option>
                                }
                                else
                                {
                                    <option value="检验通过">检验通过</option>
                                }
                                @if (Model.TestingResult == "检验不通过")
                                {
                                    <option value="检验不通过" selected>检验不通过</option>
                                }
                                else
                                {
                                    <option value="检验不通过">检验不通过</option>
                                }
                                @if (Model.TestingResult == "合格")
                                {
                                    <option value="合格" selected>合格</option>
                                }
                                else
                                {
                                    <option value="合格">合格</option>
                                }
                                @if (Model.TestingResult == "不合格")
                                {
                                    <option value="不合格" selected>不合格</option>
                                }
                                else
                                {
                                    <option value="不合格">不合格</option>
                                }
                                @if (Model.TestingResult == "待复检")
                                {
                                    <option value="待复检" selected>待复检</option>
                                }
                                else
                                {
                                    <option value="待复检">待复检</option>
                                }
                            </select>
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label required">检测人员</label>
                        <div class="layui-input-block">
                            <input type="text" name="TestingPerson" value="@Model.TestingPerson" lay-verify="required" placeholder="请输入检测人员" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    
                    <div class="layui-form-item">
                        <label class="layui-form-label">单据状态</label>
                        <div class="layui-input-block">
                            <select name="State">
                                @if (Model.State == 0)
                                {
                                    <option value="0" selected>待检验</option>
                                }
                                else
                                {
                                    <option value="0">待检验</option>
                                }
                                @if (Model.State == 1)
                                {
                                    <option value="1" selected>检验中</option>
                                }
                                else
                                {
                                    <option value="1">检验中</option>
                                }
                                @if (Model.State == 2)
                                {
                                    <option value="2" selected>检验完成</option>
                                }
                                else
                                {
                                    <option value="2">检验完成</option>
                                }
                                @if (Model.State == 3)
                                {
                                    <option value="3" selected>已审核</option>
                                }
                                else
                                {
                                    <option value="3">已审核</option>
                                }
                            </select>
                        </div>
                    </div>
                    
                    <div class="form-buttons">
                        <button type="submit" class="layui-btn" lay-submit lay-filter="submitForm">
                            <i class="layui-icon layui-icon-ok"></i>更新
                        </button>
                        <button type="button" class="layui-btn layui-btn-warm" onclick="window.history.back()">
                            <i class="layui-icon layui-icon-return"></i>返回
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script src="~/scripts/jquery-3.4.1.min.js"></script>
    <script src="~/scripts/layui-v2.5.6/layui/layui.js"></script>
    <script>
        layui.use(['form', 'laydate', 'layer'], function(){
            var form = layui.form;
            var laydate = layui.laydate;
            var layer = layui.layer;
            
            // 初始化日期选择器
            laydate.render({
                elem: '#testingDate',
                type: 'datetime',
                format: 'yyyy-MM-dd HH:mm:ss'
            });
            
            // 监听表单提交
            form.on('submit(submitForm)', function(data){
                var formData = data.field;
                
                // 验证表单数据
                if (!formData.ProcessCode.trim()) {
                    layer.msg('请输入检验单编号', {icon: 7});
                    return false;
                }
                
                if (!formData.ProcessType) {
                    layer.msg('请选择检验类型', {icon: 7});
                    return false;
                }
                
                if (!formData.OrderCode.trim()) {
                    layer.msg('请输入工单编号', {icon: 7});
                    return false;
                }
                
                if (!formData.ProductCode.trim()) {
                    layer.msg('请输入产品物料编码', {icon: 7});
                    return false;
                }
                
                if (!formData.ProductName.trim()) {
                    layer.msg('请输入产品物料名称', {icon: 7});
                    return false;
                }
                
                if (!formData.TestingNum || formData.TestingNum <= 0) {
                    layer.msg('请输入有效的检测数量', {icon: 7});
                    return false;
                }
                
                if (!formData.TestingDate) {
                    layer.msg('请选择检测日期', {icon: 7});
                    return false;
                }
                
                if (!formData.TestingResult) {
                    layer.msg('请选择检测结果', {icon: 7});
                    return false;
                }
                
                if (!formData.TestingPerson.trim()) {
                    layer.msg('请输入检测人员', {icon: 7});
                    return false;
                }
                
                // 转换数据类型
                formData.Id = parseInt(formData.Id);
                formData.TestingNum = parseInt(formData.TestingNum);
                formData.State = parseInt(formData.State);
                formData.TestingDate = new Date(formData.TestingDate).toISOString();
                
                // 提交数据
                var loadingIndex = layer.load(2, {shade: [0.3, '#000']});
                
                $.ajax({
                    url: '/Process/UpdateProcess',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(formData),
                    headers: {
                        'RequestVerificationToken': $('input[name="__RequestVerificationToken"]').val()
                    },
                    success: function(response) {
                        layer.close(loadingIndex);
                        if (response.success) {
                            layer.msg('修改成功', {icon: 1}, function() {
                                window.location.href = '/Process/Getproce';
                            });
                        } else {
                            layer.msg(response.message || '修改失败', {icon: 2});
                        }
                    },
                    error: function(xhr, status, error) {
                        layer.close(loadingIndex);
                        console.error('提交失败:', error);
                        layer.msg('网络错误，请稍后重试', {icon: 2});
                    }
                });
                
                return false; // 阻止表单默认提交
            });
        });
        
        // 选择工单功能
        function selectWorkOrder() {
            layui.use(['layer', 'table'], function() {
                var layer = layui.layer;
                var table = layui.table;
                
                console.log('开始显示工单选择弹出框');
                
                // 创建弹出框HTML内容
                var dialogHtml = `
                    <div style="padding: 20px;">
                        <div class="layui-form">
                            <div class="layui-form-item">
                                <div class="layui-inline">
                                    <label class="layui-form-label">工单编码</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="searchWorkOrderCode" class="layui-input" placeholder="请输入工单编码">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label">工单名称</label>
                                    <div class="layui-input-inline">
                                        <input type="text" name="searchWorkOrderName" class="layui-input" placeholder="请输入工单名称">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <button type="button" class="layui-btn" id="searchWorkOrderBtn">
                                        <i class="layui-icon layui-icon-search"></i> 搜索
                                    </button>
                                    <button type="button" class="layui-btn layui-btn-primary" id="resetWorkOrderBtn">
                                        <i class="layui-icon layui-icon-refresh"></i> 重置
                                    </button>
                                </div>
                            </div>
                        </div>
                        <table id="workOrderEditTable" lay-filter="workOrderEditTable"></table>
                    </div>
                `;
                
                // 打开弹出框
                var layerIndex = layer.open({
                    type: 1,
                    title: '选择工单',
                    area: ['1200px', '600px'],
                    content: dialogHtml,
                    success: function(layero, index) {
                        console.log('弹出框打开成功，开始初始化表格');
                        
                        // 初始化表格
                        var tableIns = table.render({
                            elem: '#workOrderEditTable',
                            url: '/Process/GetProductionWorkOrderList',
                            method: 'get',
                            page: true,
                            limit: 10,
                            cols: [[
                                {type: 'radio', width: 50},
                                {field: 'workOrderCode', title: '工单编码', width: 160},
                                {field: 'workOrderName', title: '工单名称', width: 200},
                                {field: 'productCode', title: '产品编号', width: 120},
                                {field: 'productName', title: '产品名称', width: 200},
                                {field: 'productModel', title: '规格型号', width: 120},
                                {field: 'unit', title: '单位', width: 80},
                                {field: 'workOrderQuantity', title: '工单数量', width: 100},
                                {field: 'customerName', title: '客户名称', width: 160}
                            ]],
                            parseData: function(res) {
                                console.log('工单数据响应:', res);
                                
                                // 检查响应数据格式
                                if (res && Array.isArray(res)) {
                                    return {
                                        "code": 0,
                                        "msg": "",
                                        "count": res.length,
                                        "data": res
                                    };
                                } else if (res && res.data && Array.isArray(res.data)) {
                                    return {
                                        "code": 0,
                                        "msg": "",
                                        "count": res.count || res.data.length,
                                        "data": res.data
                                    };
                                } else {
                                    console.error('未知的数据格式:', res);
                                    return {
                                        "code": 1,
                                        "msg": "数据格式错误",
                                        "count": 0,
                                        "data": []
                                    };
                                }
                            },
                            done: function(res, curr, count) {
                                console.log('表格渲染完成:', res, '当前页:', curr, '总数:', count);
                            }
                        });

                        // 监听行选择事件
                        table.on('radio(workOrderEditTable)', function(obj) {
                            console.log('选中工单:', obj.data);
                            var data = obj.data;
                            
                            // 填充表单数据
                            $('input[name="OrderCode"]').val(data.workOrderCode || '');
                            $('input[name="ProductCode"]').val(data.productCode || '');
                            $('input[name="ProductName"]').val(data.productName || '');
                            $('input[name="Specifications"]').val(data.productModel || '');
                            $('input[name="Unitt"]').val(data.unit || '');
                            
                            // 关闭弹出框
                            layer.close(layerIndex);
                            
                            // 显示成功消息
                            layer.msg('工单选择成功', {icon: 1});
                        });

                        // 搜索按钮事件
                        $('#searchWorkOrderBtn').off('click').on('click', function() {
                            console.log('执行搜索');
                            var searchData = {
                                workOrderCode: $('input[name="searchWorkOrderCode"]').val(),
                                workOrderName: $('input[name="searchWorkOrderName"]').val()
                            };
                            console.log('搜索条件:', searchData);
                            
                            // 重新加载表格
                            table.reload('workOrderEditTable', {
                                where: searchData,
                                page: {
                                    curr: 1
                                }
                            });
                        });

                        // 重置按钮事件
                        $('#resetWorkOrderBtn').off('click').on('click', function() {
                            console.log('重置搜索条件');
                            $('input[name="searchWorkOrderCode"]').val('');
                            $('input[name="searchWorkOrderName"]').val('');
                            
                            // 重新加载表格
                            table.reload('workOrderEditTable', {
                                where: {},
                                page: {
                                    curr: 1
                                }
                            });
                        });
                    }
                });
            });
        }
        
        // 自动生成检验单编号
        function generateProcessCode() {
            var now = new Date();
            var year = now.getFullYear();
            var month = ('0' + (now.getMonth() + 1)).slice(-2);
            var day = ('0' + now.getDate()).slice(-2);
            var hours = ('0' + now.getHours()).slice(-2);
            var minutes = ('0' + now.getMinutes()).slice(-2);
            var seconds = ('0' + now.getSeconds()).slice(-2);
            
            // 生成格式：PC + 年月日时分秒
            var processCode = 'PC' + year + month + day + hours + minutes + seconds;
            
            // 设置到输入框
            $('input[name="ProcessCode"]').val(processCode);
            
            // 显示成功提示
            layui.use(['layer'], function() {
                var layer = layui.layer;
                layer.msg('检验单编号已自动生成', {icon: 1, time: 1500});
            });
        }
    </script>
</body>
</html> 